# 使用Python生成一个方法用于统计一个字符串中每个单词出现的次数，然后再调用此方法。
# def count_words(string):
#     words = string.split()
#     word_counts = {}
#     for word in words:
#         if word not in word_counts:
#             word_counts[word] = 1
#         else:
#             word_counts[word] += 1
#     return word_counts
# print(count_words("the quick brown fox jumps over the lazy dog"))

from collections import Counter
import re
def count_words(text):
    # 使用正则表达式将字符串拆分为单词列表
    words = re.findall(r'\b\w+\b', text.lower())
    # 使用Counter计算每个单词的出现次数
    word_counts = Counter(words)
    return dict(word_counts)


# 示例用法
text = "hello python hello you hello me I love python"
result = count_words(text)
print(result)
